package com.company.test22;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

/**
 * Created by Administrator on 2016.9.28.
 */
public class TestHashMap {
    public static void main(String[] args) throws IOException{
        HashMap<String,HashSet<String>> map=read("src/com/company/test22/aa.txt");

        long time=System.currentTimeMillis();
        for(int i=0;i<1000;i++){
            System.out.println(map.get("美丽"));
        }

        System.out.println(System.currentTimeMillis()-time);
    }

    public static HashMap<String,HashSet<String>> read(String address)throws IOException{
        File file=new File(address);

        Scanner input=new Scanner(file);

        HashMap<String,HashSet<String>> map=new HashMap<>();

        while (input.hasNext()){
            String line=input.nextLine();

            ArrayList<String> arrayList=segmentation(line);

            for(int i=1;i<arrayList.size();i++){
                if(map.containsKey(arrayList.get(i))){
                    HashSet<String> set=map.get(arrayList.get(i));
                    set.add(arrayList.get(0));
                }else {
                    HashSet<String> set=new HashSet<>();
                    set.add(arrayList.get(0));
                    map.put(arrayList.get(i),set);
                }
            }
        }
        input.close();
        return map;
    }

    public static ArrayList<String> segmentation(String line){
        ArrayList<String> arrayList=new ArrayList<>();

        Scanner input=new Scanner(line);

        arrayList.add(input.next());

        while (input.hasNext()){
            arrayList.add(input.next());
        }
        input.close();

        return arrayList;
    }
}